//
//  FoodsViewController.m
//  Delicious
//
//  Created by gy on 16/5/5.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "FoodsViewController.h"
#import "CarouselView.h"
#import "Carouse.h"
#import "WebViewController.h"
#import "HotAndNewTableViewCell.h"
#import "EditChoice.h"
#import "EditChoiceTableViewCell.h"
#import "HotViewController.h"
#import "AppDelegate.h"
#import "drawerViewController.h"
#import "NewViewController.h"
#import "MJRefresh.h"
#import "EditViewController.h"
@interface FoodsViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong)NSMutableArray *carouseArr;// 轮播图数组
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, copy)NSString *descrip1; //hot的label
@property (nonatomic, copy)NSString *ID1;//hot的id
@property (nonatomic, copy)NSString *imageUrl1; // hot的图片

@property (nonatomic, copy)NSString *descrip2;
@property (nonatomic, copy)NSString *ID2;
@property (nonatomic, copy)NSString *imageUrl2;
@property (nonatomic, strong) NSMutableArray *editArr; //存放编辑精选的model
@end

@implementation FoodsViewController

- (NSMutableArray *)carouseArr{
    if (!_carouseArr) {
        _carouseArr = [NSMutableArray array];
    }
    return _carouseArr;
}

- (NSMutableArray *)editArr
{
    if (!_editArr) {
        _editArr = [NSMutableArray array];
    }
    return _editArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   //  self.automaticallyAdjustsScrollViewInsets = NO;
        // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    [self getData];
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //循环引用
    __block FoodsViewController *VC = self;
    [self.tableView addHeaderWithCallback:^{
        [VC getData];
    }];

    [self.view addSubview:_tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HotAndNewTableViewCell" bundle:nil] forCellReuseIdentifier:@"hotAndNew"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EditChoiceTableViewCell" bundle:nil] forCellReuseIdentifier:@"editChoice"];
    self.navigationController.delegate = self;
}

- (void)getData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:kMainView parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self.carouseArr removeAllObjects];
        [self.editArr removeAllObjects];
        [self.tableView headerEndRefreshing];

        NSArray *arr = responseObject[@"data"][@"adverts"];
        NSMutableArray *array = [NSMutableArray array];
        NSArray *arr1 = @[arr[2],arr[4],arr[5]];
        for (NSDictionary *d in arr1) {
            Carouse *model = [[Carouse alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [array addObject:model.imageUrl];
            [self.carouseArr addObject:model];
        }

        CarouselView *carouse = [[CarouselView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200) imageURLs:array];
        self.tableView.tableHeaderView = carouse;
        carouse.imageClick = ^(NSInteger index){
            Carouse *model = self.carouseArr[index];
            NSString *urlStr = model.url;
//            self.block(urlStr);
            WebViewController *webView = [[WebViewController alloc]init];
            webView.str = urlStr;
            [self.navigationController presentViewController:webView animated:YES completion:nil];
        };
        //取出本周最热和最新菜谱id和图片
        // hot
        NSDictionary *dic1 = responseObject[@"data"][@"mostPopularOfWeek"];
        self.descrip1 = dic1[@"description"];
        self.ID1 = dic1[@"id"];
        self.imageUrl1 = dic1[@"imageUrl"];
        //New
        NSDictionary *dic2 = responseObject[@"data"][@"newCookbook"];
        self.descrip2 = dic2[@"description"];
        self.ID2 = dic2[@"id"];
        self.imageUrl2 = dic2[@"imageUrl"];
        // editchoice
        
        NSDictionary *d1 = responseObject[@"data"][@"recommend"];
        NSDictionary *d2 = responseObject[@"data"][@"breakfast"];
        NSDictionary *d3 = responseObject[@"data"][@"lunch"];
        NSDictionary *d4 = responseObject[@"data"][@"dinner"];
        NSArray *editArr = @[d1, d2, d3, d4];
        for (NSDictionary *d in editArr) {
            EditChoice *model = [[EditChoice alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [self.editArr addObject:model];
        }


        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *str = nil;
    if (section == 1) {
        str = @"编辑精选";
    }
    return str;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return self.editArr.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableVie
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 180;
    }else{
        return 80;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HotAndNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotAndNew" forIndexPath:indexPath];
        [cell.hotImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl1]];
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]init];
        cell.hotImageView.userInteractionEnabled = YES;
        [cell.hotImageView addGestureRecognizer:tap1];
        [tap1 addTarget:self action:@selector(nextView1)];
        [cell.NewImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl2]];
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]init];
        [cell.NewImageView addGestureRecognizer:tap2];
        cell.NewImageView.userInteractionEnabled= YES;
        [tap2 addTarget:self action:@selector(nextView2)];
        cell.hotLabel.text = self.descrip1;
        cell.NewLabel.text = self.descrip2;
        return cell;
    }else {
        EditChoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"editChoice" forIndexPath:indexPath];
        EditChoice *model = self.editArr[indexPath.row];
        cell.model = model;
        return cell;
    }
}
- (void)nextView1{
    HotViewController *vc = [[HotViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)nextView2{
    NewViewController *vc = [[NewViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];

    
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    AppDelegate *delete = [UIApplication sharedApplication].delegate;
    drawerViewController *vc = (drawerViewController *)delete.window.rootViewController;


    if (navigationController.childViewControllers.count >= 2) {
        [vc close];
        // 控制器push后移除手势
        [vc.mainView removeGestureRecognizer:vc.pan];
    } else {
        // 回到跟视图时在添加上去
        [vc.mainView addGestureRecognizer:vc.pan];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EditViewController *vc = [[EditViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.model = self.editArr[indexPath.row];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
